package com.msos.service.ManagerService;

import com.msos.dao.managerDao.ManagerDao;
import com.msos.model.manager.Manager;
import com.msos.utils.CommonsUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;

/**
 * 弟中弟：小邦
 */
public class ManagerService {

    ManagerDao managerDao = new ManagerDao();

    public Integer login(String name, String password, HttpServletRequest req){

        try {
            //获取登录请求传来的验证码
            String vcode = req.getParameter("vcode");
            //用 session 保存验证码
            HttpSession session = req.getSession();
            //获取 session 中保存的验证码
            String sessionVcode = (String) session.getAttribute("vcode");
            //用 equalsIgnoreCase 取消验证码的大小写
            if(vcode == null || !vcode.equalsIgnoreCase(sessionVcode)){
                //如果验证码为空或者验证码不正确，那么取消登录
                return CommonsUtils.BAD_VCODE;
            }

            Manager manager = managerDao.getManagerByName(name);
            if(manager == null){
                //表示用户不存在
                return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
            }else {
                //用户信息不为null，表示用户存在
                if(!manager.getEnabled()){
                    //代表账户被封禁，跳过检查密码直接return返回
                    return CommonsUtils.USER_BLOCKED;
                }else {
                    //代表账户存在而且没被禁用，开始检查密码
                    String role = req.getParameter("role");
                    if(role.equals(manager.getRole().toString())){
                        if(password.equals(manager.getPassword())){
                            //密码正确，允许登录,并且将用户信息存入session
                            session.setAttribute("loginManager",manager);
                            return CommonsUtils.LOGIN_SUCCESS;
                        }else {
                            //密码错误，return返回
                            return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
                        }
                    }else {
                        return CommonsUtils.ROLE_ERROR;
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //其他异常错误
        return CommonsUtils.EXCEPTION_ERROR;
    }

    //修改密码
    public Integer updateManagerByPassword(String name,String password,String newPassword1,String newPassword2,HttpServletRequest req){
        try {
            Manager manager = managerDao.getManagerByName(name);
            if(manager == null){
                //表示用户不存在
                return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
            }else {
                //用户信息不为null，表示用户存在
                if(!manager.getEnabled()){
                    //代表账户被封禁，跳过检查密码直接return返回
                    return CommonsUtils.USER_BLOCKED;
                }else {
                    //代表账户存在而且没被禁用，开始检查密码
                    if(password.equals(manager.getPassword())){
                        if(newPassword1.equals(newPassword2)){
                            managerDao.updatePasswordByManager(newPassword2);
                            HttpSession session = req.getSession();
                            session.invalidate();
                            return CommonsUtils.UPDATE_PASSWORD_SUCCESS;
                        }else {
                            return CommonsUtils.UPDATE_PASSWORD_ERROR;
                        }
                    }else {
                        //密码错误，return返回
                        return CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD;
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }
}
